/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.main.configurator.scripts;

import java.io.IOException;

import it.gashale.jacolib.console.StandardIOConsole;
import it.gashale.jacolib.console.TmpFileConsole;
import it.gashale.jacolib.core.JacolibError;
import it.gashale.jacolib.main.Jacolib;
import it.gashale.jacolib.main.configurator.Configurator;
import it.gashale.jacolib.process.cl.CLProcess;
import it.gashale.jacolib.shell.RemoteShell;

public class CLProcessConfigurator extends Configurator {
	public void configure(Jacolib jacolib) throws JacolibError  {
		RemoteShell shell=makePeerProxy(jacolib);
		configure(shell);
	}
	
	public RemoteShell makePeerProxy(Jacolib jacolib) throws JacolibError {
		RemoteShell rs=null;
		
		String name=getValue(new String[]{"Name"},null);
		String cl=getGlobalValue(new String[]{"CL"},null);
		cl=getValue(new String[]{"CL"},cl);
		
		String startfile=getGlobalValue(new String[]{"CL","Start", "File"},"");
		startfile=getValue(new String[]{"Start","File"},startfile);
		if(startfile.equals("")) startfile=null;
		
		
		String startfilep_str=getGlobalValue(new String[]{"CL","Start", "File", "P"},"");
		startfilep_str=getValue(new String[]{"Start","File","P"},startfilep_str);
		Boolean startfilep=startfilep_str.equals("true");
		if(!startfilep) {
			startfile=null;
		}
		
		String startres=getGlobalValue(new String[]{"CL","Start", "Resource"},"");
		startres=getValue(new String[]{"Start","Resource"},startres);
		if(startres.equals("")) startres=null;
		
		String startresourcep_str=getGlobalValue(new String[]{"CL","Start", "Resource", "P"},"");
		startresourcep_str=getValue(new String[]{"Start","Resource","P"},startresourcep_str);
		Boolean startresourcep=startresourcep_str.equals("true");
		if(!startresourcep) {
			startres=null;
		}
		
		String binpaths=getGlobalValue(new String[]{"CL","Bin", "Paths"},"");
		binpaths=getValue(new String[]{"Bin","Paths"},binpaths);
		
		String port_str=getGlobalValue(new String[]{"Java","Port"}, null);
		port_str=getValue(new String[]{"Port"}, port_str);
		int port=Integer.parseInt(port_str);
		
		String consolek=getGlobalValue(new String[]{"CL","Console"}, null);
		consolek=getValue(new String[]{"Console"}, consolek);
		
		String programname=null;
		String load_option=null;
		String options=null;
		
		if(cl.equals("clisp")) {
			programname=getGlobalValue(new String[]{"Clisp", "Program", "Name"},null);
			load_option=getGlobalValue(new String[]{"Clisp", "Load", "Option"},"");
			options=getGlobalValue(new String[]{"Clisp", "Options"},"");
		} else if(cl.equals("cmucl")) {
			programname=getGlobalValue(new String[]{"CMUCL", "Program", "Name"},null);
			load_option=getGlobalValue(new String[]{"CMUCL", "Load", "Option"},"");
			options=getGlobalValue(new String[]{"CMUCL", "Options"},"");
		} else if(cl.equals("sbcl")) {
			programname=getGlobalValue(new String[]{"SBCL", "Program", "Name"},null);
			load_option=getGlobalValue(new String[]{"SBCL", "Load", "Option"},"");
			options=getGlobalValue(new String[]{"SBCL", "Options"},"");
		} else if(cl.equals("ecl")) {
			programname=getGlobalValue(new String[]{"ECL", "Program", "Name"},null);
			load_option=getGlobalValue(new String[]{"ECL", "Load", "Option"},"");
			options=getGlobalValue(new String[]{"ECL", "Options"},"");
		} else if(cl.equals("abcl")) {
			programname=getGlobalValue(new String[]{"ABCL", "Program", "Name"},null);
			load_option=getGlobalValue(new String[]{"ABCL", "Load", "Option"},"");
			options=getGlobalValue(new String[]{"ABCL", "Options"},"");
		}
		options=getValue(new String[]{"Options"},options);
		programname=getValue(new String[]{"Program","Name"},programname);
		CLProcess process=new CLProcess(programname,binpaths,options,load_option,startfile,startres,port);
		if(consolek.equals("tmp")) {
			try {
				process.setConsole(new TmpFileConsole());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(consolek.equals("std")) {
			process.setConsole(new StandardIOConsole());
		}
		rs=jacolib.connect_to_process(name, process);
		return rs;
	}
	public void configure(RemoteShell shell) throws JacolibError {
		String init_file=getGlobalValue(new String[]{"CL","Init","Source"}, "");
		init_file=getValue(new String[]{"Init","Source"}, init_file);
		if(init_file!=null && !init_file.equals("")) {
			shell.source(init_file);
		}
		String init_eval=getGlobalValue(new String[]{"CL","Init","Eval"}, "");
		init_eval=getValue(new String[]{"Init","Eval"}, init_eval);
		if(init_eval!=null && !init_eval.equals("")) {
			shell.eval(init_eval);
		}
	}
}
